package ee.siimkaspar.subjects.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.dispatcher.SessionMap;

import com.opensymphony.xwork2.ActionInvocation;

public class SessionInterceptor extends BaseInterceptor {

	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		Logger.getLogger(getClass()).debug("SESSION INTERCEPTOR");
		HttpServletRequest request = (HttpServletRequest) ai.getInvocationContext().get(HTTP_REQUEST);
		HttpSession httpSession = request.getSession(false);
		if (httpSession == null && !request.isRequestedSessionIdValid()) {
			((SessionMap<?,?>) ai.getInvocationContext().getSession()).invalidate();
			return SESSION_EXPIRED;
		}
		return ai.invoke();
	}

}
